# -*- coding: utf-8 -*-
# @Time    : 2019/5/17 20:57
# @Author  : awer
# @Email   : awer@awer.cn
# @File    : proc_msg.py
# @Software: PyCharm
import db_helper
import helper
import jx3_com
import sign_helper
import tuling


def proc_private_msg(cq, params):
    from_qq = params['from_qq']
    msg = params['msg'].strip()
    re_msg = tuling.tuling(msg)
    return re_msg


def proc_group_msg(cq, params):
    group_id = params['from_group']
    msg = params['msg'].strip()
    cfg = helper.get_group_option(group_id)

    if cfg['enable_sign'] or cfg is None:
        if msg.lower() in {'签到', '报到'} or msg.startswith('[签到]') or msg.startswith('[CQ:sign'):
            try:
                u_power = sign_helper.get_rand_points()
                db_helper.init(params['env']['CQ_HOME'])
                re_json = db_helper.user_sign(params['from_qq'], group_id, u_power)
                re_msg = helper.str_at(params['from_qq']) + '\r\n' + sign_helper.get_sign_msg(re_json)

            except BaseException as e:
                cq.logging.debug(str(e))
                re_msg = helper.str_at(params['from_qq']) + '\r\n' + sign_helper.get_sign_msg_fail()
            return re_msg

    if cfg['only_atme']:
        if msg.find(helper.str_at_me()) < 0: return None

    if msg.lower() in {'帮助', 'help'}:
        return helper.get_help()

    if cfg['enable_qiyu']:
        if msg.strip() == '奇遇':
            return jx3_com.get_last_qiyu()
        else:
            msg_type = msg[0:2]
            item = msg[2:].strip()

            if msg_type == '奇遇':
                qiyu = jx3_com.search_qiyu(item)
                if len(qiyu) > 0:
                    return helper.str_at(params['from_qq']) + '\r\n' + qiyu

    if cfg['enable_price']:
        if msg.startswith('金价') or msg.find('金币价格') >= 0:
            return jx3_com.get_price_coin()

    if cfg['enable_daily']:
        if msg.strip() == '日常':
            return helper.str_at(params['from_qq']) + '\r\n' + jx3_com.get_daily()

    if cfg['enable_server']:
        if msg in {'开服', '亢龙有悔', '亢龙'}:
            return jx3_com.get_server_status()

    # @me
    if msg.find(helper.str_at_me()) >= 0 or msg.find(helper.ROBOT_NAME) >= 0:
        msg = msg.replace(helper.str_at_me(), '')
        if msg.find('闭嘴') >= 0 or msg.find('关闭') >= 0 or msg.find('休息') >= 0 or msg.find('stop') >= 0:
            helper.silent_time = helper.get_second_stamp() + 600
            re_msg = '%s我先休息10分钟，不要想我啊' % helper.str_emoji(128539)
            return helper.str_at(params['from_qq']) + '\r\n' + re_msg
        else:
            return helper.str_at(params['from_qq']) + '\r\n' + tuling.tuling(msg)

    # random reply
    if cfg['enable_random_reply'] and not helper.is_silent():
        if msg.find('[') < 0 and len(msg) > 15 and helper.get_random_rate(3):
            return tuling.tuling(msg)

    #  one plus
    if len(msg) > 3 and not helper.is_silent():
        last_msg = helper.get_repeat_msg(group_id, msg)
        cq.logging.debug(str(helper.last_msg_list))
        if last_msg is not None:
            return msg
